const puppeteer = require('puppeteer');
const fs = require('fs');

let totalBlogs = [];
let errLogs = [];

async function main() {
  let totalBlogs = [];
  const MAX_CNT = 300;
  const SLEEP_TIME = 200;

  const tab = 'other';
  const sub_tab = 'javastack';
  const fileName = `javastack.json`;


  const browser = await puppeteer.launch({headless: true, slowMo: 0});
  const page = (await browser.pages())[0];
  await page.setViewport({width: 1500, height: 0});
  page.on('console', msg => {
    console.log(msg.text());
  });

  page.on('pageerror', msg => {
    console.log(msg);
  });


  await page.exposeFunction('dateDiff', (nowDate, diffString) => {
    function _fmtDate(obj, needTime, justDate) {
      var date = new Date(obj);
      var y = 1900 + date.getYear();
      var m = "0" + (date.getMonth() + 1);
      var d = "0" + date.getDate();
      var h = "0" + date.getHours();
      var min = "0" + date.getMinutes();
      var s = "0" + date.getSeconds();
      if (justDate) {
        return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length);
      }
      if (needTime) {
        return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length) +
          ' ' + h.substring(h.length - 2, h.length) + ":00:00";
      } else {
        return y + "-" + m.substring(m.length - 2, m.length) + "-" + d.substring(d.length - 2, d.length) + ' 00:00:00';
      }
    }

    function _dateDiff(nowDate, diffString) {
      nowDate = new Date(nowDate);

      const minuteReg = /分钟前$/;
      const hourReg = /小时前$/;
      const dayReg = /天前$/;
      const monthReg = /月前$/;
      const yearReg = /年前$/;
      const yyyymmddReg = /年.+月.+日/;
      const mmddReg = /^\d+月.+日/;
      const todayReg = /^今天/;
      const yesterdayReg = /^昨天/;
      const beforeYesterdayReg = /^前天/;
      const exceptYearReg = /^(\d{0,9}\/{1}\d{0,9})\s(\d{0,9}\:{1}\d{0,9})/; // 匹配形如 03/15 15:19
      const slashReg = /^(\d{0,9}\/{1}\d{0,9}\/{1}\d{0,9})\s(\d{0,9}\:{1}\d{0,9})/; // 匹配形如 2018/03/15 15:19
      const normReg = /\d{4}\-\d{1,2}\-\d{1,2}/;

      if (minuteReg.test(diffString)) {
        return _fmtDate(nowDate.getTime(), true);
      }

      if (hourReg.test(diffString)) {
        const hourTimestamp = Number(diffString.split('小时前')[0]) * 3600 * 1000;
        return _fmtDate(nowDate.getTime() - hourTimestamp, true);
      }
      if (dayReg.test(diffString)) {
        const dayTimestamp = Number(diffString.split('天前')[0]) * 24 * 3600 * 1000;
        return _fmtDate(nowDate.getTime() - dayTimestamp, false);
      }
      if (monthReg.test(diffString)) {
        const monthTimestamp = Number(diffString.split('月前')[0]) * 30 * 24 * 3600 * 1000;
        return _fmtDate(nowDate.getTime() - monthTimestamp, false);
      }
      if (yearReg.test(diffString)) {
        const yearTimestamp = Number(diffString.split('年前')[0]) * 365 * 24 * 3600 * 1000;
        return _fmtDate(nowDate.getTime() - yearTimestamp, false);
      }
      if (yyyymmddReg.test(diffString)) {
        return _fmtDate(new Date(Date.parse(diffString.replace('年', '-').replace('月', '-').replace('日', ''))), false);
      }
      if (mmddReg.test(diffString)) {
        let yearStr = (new Date()).getFullYear() + '年';
        return _fmtDate(new Date(Date.parse((yearStr + diffString).replace('年', '-').replace('月', '-').replace('日', ''))), false);
      }
      if (todayReg.test(diffString)) {
        const timeString = diffString.split('今天')[1].replace(/(^\s*)|(\s*$)/g, "")
        return _fmtDate(nowDate.getTime(), false, true) + ' ' + timeString + ':00';
      }
      if (yesterdayReg.test(diffString)) {
        const timeString = diffString.split('昨天')[1].replace(/(^\s*)|(\s*$)/g, "")
        const timestamp = nowDate.getTime() - 24 * 3600 * 1000;
        return _fmtDate(timestamp, false, true) + ' ' + timeString + ':00';
      }
      if (beforeYesterdayReg.test(diffString)) {
        const timeString = diffString.split('前天')[1].replace(/(^\s*)|(\s*$)/g, "")
        const timestamp = nowDate.getTime() - 2 * 24 * 3600 * 1000;
        return _fmtDate(timestamp, false, true) + ' ' + timeString + ':00';
      }
      if (exceptYearReg.test(diffString)) {
        const dateString = (1900 + nowDate.getYear()) + '-' + diffString.replace(/\//g, "-") + ':00';
        return dateString;
      }
      if (slashReg.test(diffString)) {
        const dateString = diffString.replace(/\//g, "-") + ':00';
        return dateString;
      }

      if (normReg.test(diffString)) {
        return diffString + " 00:00:00";
      }

      return '时间格式有误';
    }

    return _dateDiff(nowDate, diffString);
  });

  let catId = 10;
  let pageSize = 30;
  for (let i = 0; i < pageSize; i++) {
    try {
      let url = null;
      if (i == 0) {
        url = 'https://www.javastack.cn/archives';
      } else {
        url = `https://www.javastack.cn/archives/page/${i + 1}`;
      }
      await page.goto(url);
      await page.waitFor(1000);
      let blogs = await page.$$eval('.timeline-row:not(.timeline-row-major)', async (eles) => {
        let tmpBlogs = [];
        eles.forEach(async ele => {
          let title = ele.querySelector('div > h1 > a.timeline-article-title').innerText;
          let content = ele.querySelector('div > h1 > a.timeline-article-title').href;
          let old_date = ele.querySelector('div > div > div.article-date > a > time').innerText;
          let blog_date = await window.dateDiff(new Date(), old_date);
          tmpBlogs.push({
            title: title,
            content: content,
            tab: 'other',
            sub_tab: 'javastack',
            blog_date: blog_date
          });
        });
        return tmpBlogs;
      });
      console.log(blogs);
      console.log('--------------');
      totalBlogs = totalBlogs.concat(blogs);
    } catch (e) {
      console.log(e.message);
      errLogs.push(url);
      continue;
    }
  }


  let writerStream = fs.createWriteStream(`crawler/crawler_data/${fileName}`);
  writerStream.write(JSON.stringify(totalBlogs));
  console.log('write done');

  console.log(errLogs);

  // browser.close();
};


main();


